package at.spengergasse.web.servlet.kunden;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import at.spengergasse.model.Kunden;

@SuppressWarnings("serial")
public class DeleteKundenServlet extends AbstractKundenServlet
{

	public DeleteKundenServlet() throws Exception
	{
	}

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException
	{
		log.debug("started processing");

		Long kundenId = checkKundenIdParam(req, resp);

		Kunden kunden = kundenDAO.findById(connection, kundenId);
		if (kunden != null)
		{
			kundenDAO.delete(connection, kunden);
			try
			{
				connection.commit();
				log.info("successfully deleted kunden for id " + kundenId);
			} catch (SQLException ex)
			{
				log.warn("troubles deleting kunden with id " + kundenId);
				redirectToKundenList(req, resp, "troubles deleting kunden");
				return;
			}
		} else
		{
			log.warn("troubles finding kunden with id " + kundenId
					+ " for deletion");
			redirectToKundenList(req, resp, "could not find kunden with id "
					+ kundenId);
			return;
		}

		redirectToKundenList(req, resp);

		log.debug("finished processing");
	}
}
